using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using Pheonix.CodeLibrary.Web;

public partial class page_private_admin_membershiplist : AdminBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.Title = SiteName + " - Membership List [ Admin ]";
        LoadScripts();
    }

    private void LoadScripts()
    {
        deleteAllButton.Attributes.Add("onclick", "javascript:return confirm('Are you sure you want to delete?');");
    }

    protected void membershipList_RowCommand(object sender, CommandEventArgs e)
    {
        if (e.CommandName == "editCommand")
        {
            Response.Redirect("membershipdetail.aspx?id=" + e.CommandArgument.ToString() + "&mode=" +
                FormViewMode.Edit.ToString());     
        }
        else if (e.CommandName == "deleteCommand")
            membershipList.DeleteRow(Convert.ToInt32(e.CommandArgument)); //command argument contains row index
    }

    protected void membershipList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            CheckBox selectAll = (CheckBox)e.Row.FindControl("selectAll");
            selectAll.Attributes.Add("onclick", "SelectAll('" + membershipList.ClientID + "','" + selectAll.ClientID + "')");
        }

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Int32 id = (Int32)membershipList.DataKeys[e.Row.RowIndex].Value;

            ImageButton editButton = (ImageButton)e.Row.FindControl("editButton");
            editButton.AlternateText = "edit";
            editButton.CommandArgument = id.ToString();

            ImageButton deleteButton = (ImageButton)e.Row.FindControl("deleteButton");
            deleteButton.AlternateText = "delete";
            deleteButton.CommandArgument = e.Row.RowIndex.ToString();
            deleteButton.Attributes.Add("onclick", "javascript:return confirm('Are you sure you want to delete?');");
        }
    }

    protected void addNewButton_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("membershipdetail.aspx?mode=Insert");
    }

    protected void deleteAllButton_Click(object sender, ImageClickEventArgs e)
    {
        GridViewRowCollection membershipListRows = membershipList.Rows;
        List<int> idToDelete = new List<int>();

        foreach (GridViewRow row in membershipListRows)
        {
            CheckBox rowSelection = (CheckBox)row.FindControl("rowSelect");

            if (rowSelection.Checked)
            {
                idToDelete.Add((int)membershipList.DataKeys[row.RowIndex].Value);
            }
        }

        if (Pheonix.Fashionzoomer.Entity.SqlServer2000.Membership.DeleteMembershipList(idToDelete))
        {
            membershipList.DataBind();
        }
        else
        {

        }
    }
}
